<?php
/**
 * qMySQLQuery represents result of performed MySQL query. To create this, call qMySQLConnection::Query().
 */
class qMySQLQuery
{
    /** Array of results of select query. */
    private $res = array();
    /** Next row to be returned after Row() call. */
    private $pos = 0;
    /** Last insert id of insert query. */
    private $insertid = -1;
    /** Number of updated/deleted/replaced rows. */
    private $modified = -1;
    
    /**
     * Constructor. Do not call this directly.
     * @param string $query query to be performed
     * @param mysql_link $link connection
     * @throws Exception if not called from qMySQLConnection.
     */
    function __construct($query, $link)
    {
        // check if it's called from qMySQLConnection, if not throw Exception
        $backtrace = debug_backtrace();
        if ($backtrace[1]['class'] != "qMySQLConnection") throw new Exception();

        // perform query
        $resource = @mysql_query($query, $link);
        
        //get type of query
        $type = strtolower(substr($query, 0, 6));
        
        // if select, then get all results into $res
        if ($type == "select")
        {
            while ($row = @mysql_fetch_array($resource))
            {
                $this->res[] = $row;
            }
        }
        // if insert, get insert id
        else if ($type == "insert")
        {
            $this->insertid = @mysql_insert_id();
        }
        // if update / delete / replace, get number of modified records
        else if ($type == "update" || $type == "delete" || strtolower(substr($query, 0, 7)) = "replace")
        {
            $this->modified = @mysql_affected_rows();
        }
    }
    
    /**
     * Returns next or specified by $pos record.
     * 
     * If $pos is set, then internal position is set to it, and returns record at index $pos.
     * @param int $pos index
     * @return Data row / null 
     */
    public function Row($pos = null)
    {
        // if there is no data, return null
        if ($this->res == array())
        {
            return null;
        }
        
        // if $pos is set, set internal position to $pos
        if ($pos != null)
        {
            $this->pos = $pos;
        }
        
        // return row at internal index and then increment that index
        return $this->res[$this->pos++];
    }
    
    /**
     * Returns id of last insert performed by this query object.
     * @return Last insert id / null
     */
    public function InsertId()
    {
        // this was not insert query, return null
        if ($this->insertid == -1)
        {
            return null;
        }
        
        // return that id
        return $this->insertid;
    }
    
    /**
     * Return number of affected rows.
     * @return Number of affected rows / null
     */
    public function AffectedRows()
    {
        // this was not update / delete / replace query, return null
        if ($this->modified == -1)
        {
            return null;
        }
        
        // return number of modified rows
        return $this->modified;
    }
}